using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using WB.Entity;
using WB.Entity.Helper;
using WB.Library;

namespace WB.DaoHelper
{
	public class CategoryDao : CategoryDaoBase
	{
		/// <summary>
		/// Purpose: Class constructor.
		/// </summary>
		public static CategoryDao Instants()
		{
			return new CategoryDao();
		}

        public DataTable GetAll()
        {
            string strSQL = "shop_GetCategory";
            return ExecuteDataTable(CommandType.StoredProcedure, strSQL);
        }

        public bool CheckDelete(Guid catId)
        {
            string strSQL = "Select * " +
                            "From Products " +
                            "Where CategoryId=@catId";
            SqlParameter[] param = new SqlParameter[1];
            DataTable tbl = ExecuteDataTable(CommandType.Text, strSQL, param);
            if (tbl != null && tbl.Rows.Count > 0)
                return false;
            return true;
        }

        public DataTable GetAllParent()
        {
            string strSQL = "Select * " +
                            "From Category " +
                            "Where ParentCode is null " +
                            "Order By OrderNo";
            return ExecuteDataTable(CommandType.Text, strSQL);
        }

        public CategoryEntity GetEntityByCode(string Code)
        {
            CategoryEntity tmp = new CategoryEntity();
            return (CategoryEntity)SelectByKey(tmp, CategoryField.CatCode, Code);
        }

        public CategoryEntity GetEntityByIntId(int catId)
        {
            CategoryEntity tmp = new CategoryEntity();
            return (CategoryEntity)SelectByKey(tmp, CategoryField.IntId, catId);
        }

        public DataTable GetByCode(string Code)
        {
            string strSQL = "Select * " +
                            "From Category " +
                            "Where ParentCode=@CatCode and IsDeleted=0 " +
                            "Order By OrderNo";
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@CatCode", Code);
            return ExecuteDataTable(CommandType.Text, strSQL, param);
        }
	}
}
